package com.weichat.jwh.tool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.model.Event_user;
import com.model.Propertr_user;
import com.model.User;
import com.service.Event_userService;
import com.service.Propertr_userService;
import com.service.UserService;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.LessionTool;
@Component
public class MessageTool {
	@Autowired
	AccessTokentool accessTokentool;
	@Autowired
	UserService userService;
	@Autowired
	Propertr_userService propertr_userService;
	@Autowired
	Event_userService event_userService;
	@Autowired
	LessionTool lessionTool;
	/**
	 * 查看 公众号应用的模板消息
	 * 
	 * @return
	 * @throws IOException 
	 */
	 public   String getTemplate() throws IOException {
			
		 
		 
		 String access_token= "CrHZNUf5cyeA2eYBQW8aPzsHgOMeugJKck1JwvCjTRLsMXDPW8dkm56K18vl6Bt8XcgiHT_iPFP0oXhWrq684IQiZ1zmuFZjnHlNbT7RGswAD9XlFqcFHQjWeDckP9AoPZBgABALVX";
		 System.out.println("access_token=="+access_token);
		 String action = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="+access_token;
		 try {
		 URL url = new URL(action);
		 HttpURLConnection http = (HttpURLConnection) url.openConnection(); 

		 http.setRequestMethod("GET"); 
		 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
		 http.setDoOutput(true); 
		 http.setDoInput(true);
		 System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
		 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
		 http.connect();
 

		 InputStream is =http.getInputStream();
		 int size =is.available();
		 byte[] jsonBytes =new byte[size];
		 is.read(jsonBytes);
		 String message=new String(jsonBytes,"UTF-8");
		 return "返回信息"+message;
		 } catch (MalformedURLException e) {
		 e.printStackTrace();
		 } catch (IOException e) {
		 e.printStackTrace();
		 } 
		 return "createMenu 失败";
		 }
	 
		public     String  sendMsg(String openid,String first,String keyword1,String keyword2,String remark) throws IOException{
			 String access_token=(String)accessTokentool.getNewAccessToken().get("rs");
			 System.out.println("access_token=="+access_token);
			 String action = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
			 
			 String Msg ="{\"touser\":\""+openid+"\""
			 		+ ",\"template_id\":\"TeTuT0mwnSdQCElik1KfP5Zyxtgm3kMa8E89A_5j8r4\","
			 		+ "\"data\":{"
			 		+ "\"first\":{"
			 		+ "\"value\":\""+first+"\","
			 		+ "\"color\":\"#173177\""
			 		+ "},"
			 		+ "\"keyword1\":{"
			 		+ "\"value\":\""+keyword1+"\","
			 		+ "\"color\":\"#173177\""
			 		+ "},"
			 		+ "\"keyword2\":{"
			 		+ "\"value\":\""+keyword2+"\","
			 		+ "\"color\":\"#173177\""
			 		+ "},"
			 		+ "\"remark\":{"
			 		+ "\"value\":\""+remark+"\","
			 		+ "\"color\":\"#173177\""
			 		+ "}"
			 		+ "}"
			 		+ "}";
			 
			 try {
				 URL url = new URL(action);
				 HttpURLConnection http = (HttpURLConnection) url.openConnection(); 

				 http.setRequestMethod("POST"); 
				 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
				 http.setDoOutput(true); 
				 http.setDoInput(true);
				 System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
				 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
				 http.connect();
				 OutputStream os= http.getOutputStream(); 
				 os.write(Msg.getBytes("UTF-8"));//传入参数 
				 os.flush();
				 os.close();

				 InputStream is =http.getInputStream();
				 int size =is.available();
				 byte[] jsonBytes =new byte[size];
				 is.read(jsonBytes);
				 String message=new String(jsonBytes,"UTF-8");
				 System.out.println("message----------------"+message);
				 return "返回信息"+message;
				 } catch (MalformedURLException e) {
				 e.printStackTrace();
				 } catch (IOException e) {
				 e.printStackTrace();
				 } 
				 return " 失败";
			 
			 
			 
		}
	 
		
		public void doSendMsg(String first,String keyword1,String keyword2,int remark) throws IOException{
			List<User> userList=userService.selectOutTime(remark);
			String mark="";
			String name="";
			int count=0;
			int count1=0;
			int count2=0;
			int count3=0;
			if(remark==1){
				mark="起床任务失败//  －5元//  －100%执行能力//  －100%信誉度";
				name="早上起床";
				count=-5;

				count3=5;
			}else if(remark==2){
				mark="吃早餐任务失败//  －5元//  －100%执行能力//  －100%信誉度";
				name="吃早餐";
				count=-5;

			}else if(remark==3){
				mark="乘车不准时//  －10元//  －50%执行能力//  －50%信誉度";
				name="乘营车";
				count=-10;

			}else if(remark==4){
				mark="没有存放手机//  －本周勤学金//  －100%执行能力//  －100%信誉度";
				name="早上放手机";
				count=-5;
				
			}else if(remark==5){
				mark="没有按时午休//  －5元//  ＋0%午休睡觉执行能力//  ＋0%午休睡觉信誉度元//  ＋0%午休起床执行能力//  ＋0%午休起床信誉度";
				name="午睡";
				count3=5;
				count=-5;

			}else if(remark==6){
				mark="午休没有按时起床//  －5元//  ＋0%执行能力//  ＋0%信誉度";
				name="午睡起床";
				count3=5;
				count=-5;

			}else if(remark==7){
				mark="没有存放手机//  －本周全部勤学金//  ＋0%执行能力//  ＋0%信誉度";
				name="下午放手机";
				count=-5;
;
			}else if(remark==8){
				mark="运动健身旷训！//  －今日全部勤学金//  ＋0%执行能力//  ＋0%信誉度";
				name="环湖跑";
				count=-5;

				userList=userService.selectOutTime8910();
			}else if(remark==11){
				mark="没有存放手机//  －本周全部勤学金//  ＋0%执行能力//  ＋0%信誉度";
				name="晚上放手机";
				count=-5;


			}else if(remark==12){
				mark="睡觉";
				name="晚上上床睡觉";


			}else{
				
			}
			
			for(User u:userList){
				sendMsg(u.getOpenid(),first,keyword1,keyword2,mark);
				
			 }
			
		}

		public void doSendMsg2(String first,String keyword1,String keyword2,int remark) throws IOException{
			//List<User> userList=userService.selectAll();
			List<Propertr_user> pu=propertr_userService.selectU_id();
			System.out.println("========"+pu.size());
			String mark="";
			String name="";
			int count=0;
			int count1=0;
			int count2=0;
			int count3=0;
			if(remark==1){
				mark="起床任务失败//  －5元//  －100%执行能力//  －100%信誉度";
				name="早上起床";
				count=-5;

				count3=5;
			}else if(remark==2){
				mark="吃早餐任务失败//  －5元//  －100%执行能力//  －100%信誉度";
				name="吃早餐";
				count=-5;

			}else if(remark==3){
				mark="乘车不准时//  －10元//  －50%执行能力//  －50%信誉度";
				name="乘营车";
				count=-10;

			}else if(remark==4){
				mark="没有存放手机//  －本周勤学金//  －100%执行能力//  －100%信誉度";
				name="早上放手机";
				count=-5;
				
			}else if(remark==5){
				mark="没有按时午休//  －5元//  ＋0%午休睡觉执行能力//  ＋0%午休睡觉信誉度元//  ＋0%午休起床执行能力//  ＋0%午休起床信誉度";
				name="午睡";
				count3=5;
				count=-5;

			}else if(remark==6){
				mark="午休没有按时起床//  －5元//  ＋0%执行能力//  ＋0%信誉度";
				name="午睡起床";
				count3=5;
				count=-5;

			}else if(remark==7){
				mark="没有存放手机//  －本周全部勤学金//  ＋0%执行能力//  ＋0%信誉度";
				name="下午放手机";
				count=-5;
;
			}else if(remark==8){
				mark="运动健身旷训！//  －今日全部勤学金//  ＋0%执行能力//  ＋0%信誉度";
				name="环湖跑";
				count=-5;

			//	userList=userService.selectOutTime8910();
			}else if(remark==11){
				mark="没有存放手机//  －本周全部勤学金//  ＋0%执行能力//  ＋0%信誉度";
				name="晚上放手机";
				count=-5;


			}else if(remark==12){
				mark="睡觉";
				name="晚上上床睡觉";


			}else{
				
			}
			
			for(int i=0;i<pu.size();i++){
			
				System.out.println("================="+pu.get(i).getuId());
				Calendar now = Calendar.getInstance();
				SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
				//String str =sd.format(now);
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
				String str = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
				if(event_userService.selectByUidEid(pu.get(i).getuId(), remark)==null){
					Event_user eu=new Event_user();
					eu.setUserId(pu.get(i).getuId());
					eu.setEventId(remark);
					eu.setType("N");	
					event_userService.insertSelective(eu);
					Propertr_user p1=new Propertr_user();
					int eu_id=event_userService.selectEidByUidOrder(pu.get(i).getuId()).getEuId();
					p1.setuId(pu.get(i).getuId());
					p1.setpId(1);
					p1.setCount(count);
					p1.setEuId(eu_id);	
					p1.setEventName(name);
					Propertr_user p2=new Propertr_user();			
					p2.setuId(pu.get(i).getuId());
					p2.setpId(2);
					p2.setCount(count1);
					p2.setEuId(eu_id);	
					p2.setEventName(name);
					Propertr_user p3=new Propertr_user();
					p3.setuId(pu.get(i).getuId());
					p3.setpId(3);
					p3.setCount(count2);
					p3.setEuId(eu_id);	
					p3.setEventName(name);
					propertr_userService.insertSelective(p1);
					propertr_userService.insertSelective(p2);
					propertr_userService.insertSelective(p3);
				}else{
					
					
					String str1 =sd.format(event_userService.selectByUidEid(pu.get(i).getuId(), remark).getUpdateDt().getTime());
					System.out.println("==========时间1"+str+"============时间2"+str1);
					
					
					if(str.equals(str1)){
						
//			//	}else if(lessionTool.judgeLession(p.getuId())){
//					Event_user eu=new Event_user();
//					eu.setUserId(p.getuId());
//					eu.setEventId(remark);
//					eu.setType("Y");	
//					event_userService.insertSelective(eu);
//					Propertr_user p1=new Propertr_user();
//					int eu_id=event_userService.selectEidByUidOrder(p.getuId()).getEuId();
//				//	if(0!=count3){
//					p1.setuId(p.getuId());
//					p1.setpId(1);
//					p1.setCount(count3);
//					p1.setEuId(eu_id);	
//					p1.setEventName(name);
//					propertr_userService.insertSelective(p1);
//				//	}
//					Propertr_user p2=new Propertr_user();			
//					p2.setuId(p.getuId());
//					p2.setpId(2);
//					p2.setCount(100);
//					p2.setEuId(eu_id);	
//					p2.setEventName(name);
//					Propertr_user p3=new Propertr_user();
//					p3.setuId(p.getuId());
//					p3.setpId(3);
//					p3.setCount(100);
//					p3.setEuId(eu_id);	
//					p3.setEventName(name);
//					propertr_userService.insertSelective(p2);
//					propertr_userService.insertSelective(p3);
						
						
						
						
					}else{
						
						
						Event_user eu=new Event_user();
						eu.setUserId(pu.get(i).getuId());
						eu.setEventId(remark);
						eu.setType("N");	
						event_userService.insertSelective(eu);
						Propertr_user p1=new Propertr_user();
						int eu_id=event_userService.selectEidByUidOrder(pu.get(i).getuId()).getEuId();
						p1.setuId(pu.get(i).getuId());
						p1.setpId(1);
						p1.setCount(count);
						p1.setEuId(eu_id);	
						p1.setEventName(name);
						Propertr_user p2=new Propertr_user();			
						p2.setuId(pu.get(i).getuId());
						p2.setpId(2);
						p2.setCount(count1);
						p2.setEuId(eu_id);	
						p2.setEventName(name);
						Propertr_user p3=new Propertr_user();
						p3.setuId(pu.get(i).getuId());
						p3.setpId(3);
						p3.setCount(count2);
						p3.setEuId(eu_id);	
						p3.setEventName(name);
						propertr_userService.insertSelective(p1);
						propertr_userService.insertSelective(p2);
						propertr_userService.insertSelective(p3);
					}
				}
			 }
			
		}
	 
	 public static void main(String[] args) throws IOException {

			//System.out.println(deleteMenu());
				MessageTool mt=new MessageTool();
			//mt.doSendMsg2("到期提醒","晚上放手机" ,"20:10" ,11);
			
			}
}
